package com.example.wastewater.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("http://localhost:5173"); // 允许的域名
        config.addAllowedHeader("*"); // 允许所有的请求头
        config.addAllowedMethod("*"); // 允许所有的请求方法
        config.setMaxAge(3600L); // 预检请求的缓存时间
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}
